package org.fcitmuk.openclinica;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import org.fcitmuk.db.util.Persistent;
import org.fcitmuk.db.util.PersistentHelper;


/**
 * This class encapsulates a list of subject database fields.
 * 
 * @author Daniel
 *
 */
public class SubjectFieldList implements Persistent{

	private Vector fields;
	
	public SubjectFieldList(){
		
	}
	
	public SubjectFieldList(Vector subjectFields) {
		super();
		this.fields = subjectFields;
	}

	public Vector getFields() {
		return fields;
	}

	public void setFields(Vector fields) {
		this.fields = fields;
	}
	
	public void addField(SubjectField field){
		if(fields == null)
			fields = new Vector();
		fields.addElement(field);
	}
	
	public void addSubjectFields(Vector fieldList){
		if(fieldList != null){
			if(fields == null)
				fields = fieldList;
			else{
				for(int i=0; i<fieldList.size(); i++ )
					this.fields.addElement(fieldList.elementAt(i));
			}
		}
	}
	
	public int size(){
		if(getFields() == null)
			return 0;
		return getFields().size();
	}
	
	public SubjectField getField(int index){
		return (SubjectField)getFields().elementAt(index);
	}

	/** 
	 * Reads the subject field collection object from the supplied stream.
	 * 
	 * @param dis - the stream to read from.
	 * @throws IOException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public void read(DataInputStream dis) throws IOException, InstantiationException, IllegalAccessException {
		setFields(PersistentHelper.read(dis,new SubjectField().getClass()));
	}

	/** 
	 * Writes the subject field collection object to the supplied stream.
	 * 
	 * @param dos - the stream to write to.
	 * @throws IOException
	 */
	public void write(DataOutputStream dos) throws IOException {
		PersistentHelper.write(getFields(), dos);
	}
}
